<template>
  <!-- 修改回访记录弹出框 -->
  <el-dialog title="填写回访记录" v-model="editVisible" width="30%" draggable>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="150px" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="手机号：" prop="mobile">
            <el-input v-model.trim.number="form.mobile" placeholder="手机号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="充值金额：" prop="amount">
            <el-input v-model.trim="form.amount" placeholder="充值金额"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="商家备注：" prop="remark">
            <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }" placeholder="商家备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="submitForm">确 定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useState } from "@/hooks/useState.ts";
import type { IRedPacketRecharge } from "@/types/moneyManager/redPacket";
import { putRedRechargeInfo } from "@/utils/api/redPacket";
import { mobildVerify } from "@/utils/utils";
const { disabledbtn, loading, toggleState } = useState();
const emit = defineEmits<{
  handleSuccess: [];
}>();
const editVisible = defineModel<boolean>("editVisible", { required: true });
const form = defineModel<IRedPacketRecharge>("form", { required: true });
const formRef = useTemplateRef("formRef");
const rules = reactive({
  mobile: [{ required: true, validator: mobildVerify, trigger: "blur" }],
  amount: [{ required: true, message: "请填写充值金额", trigger: "blur" }]
});

watch(editVisible, newValue => {
  if (!newValue) {
    handleInitData();
  }
});

function handleInitData() {
  formRef.value?.resetFields();
  formRef.value?.clearValidate();
}
/* 保存或编辑 */
function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    submitFormInfo();
  });
}

/* 保存或编辑信息 */
function submitFormInfo() {
  toggleState(true);
  putRedRechargeInfo(form.value)
    .then(res => {
      ElMessage.success(res.msg);
      editVisible.value = false;
      emit("handleSuccess");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}
</script>

<style lang="scss" scoped></style>
